package com.block.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.block.db.entity.SourceBaseInfo;
import com.block.db.mapper.SourceBaseInfoMapper;
import com.block.common.service.ISourceBaseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 线索基本信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-12-25
 */
@Service
public class SourceBaseInfoServiceImpl extends ServiceImpl<SourceBaseInfoMapper, SourceBaseInfo> implements ISourceBaseInfoService {

    @Resource
    private RedisTemplate<String, SourceBaseInfo> redisSourceBaseInfoTemplate;

    private  static  final  String SOURCEINFO_KEY_PREFIX="SOURCE:BASEINFO:";
    private  static  final  Long SOURCEINFO_KEY_TIME_OUT=4L;

    @Override
    public SourceBaseInfo getSourceBaseInfoByCode(String channelCode){
        String redisKey = SOURCEINFO_KEY_PREFIX+channelCode;
        SourceBaseInfo sourceBaseInfo= redisSourceBaseInfoTemplate.opsForValue().get(redisKey);
        if(ObjectUtil.isEmpty(sourceBaseInfo)){
            sourceBaseInfo = this.baseMapper.selectByChannelCode(channelCode);
            if(sourceBaseInfo!=null) {
                redisSourceBaseInfoTemplate.opsForValue().set(redisKey, sourceBaseInfo, SOURCEINFO_KEY_TIME_OUT, TimeUnit.HOURS);
            }
        }
        return sourceBaseInfo;
    }

}
